package com.ruoyi.stock2.thread.aboutDownloadData;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.stock2.domain.PriceCompAverage200;
import com.ruoyi.stock2.domain.STOCK;
import com.ruoyi.stock2.mapper.PriceCompAverage200Mapper;
import com.ruoyi.stock2.service.ISTOCKService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock2.domain.ForetellResult;
import com.ruoyi.stock2.service.IForetellResultService;

/*
 * 1、创建训练集 txt文本
 *
 * 2、创建预测集 保存到mysql
 */
@Service
public class SaveToForetellResult {
	private static final Logger log = LoggerFactory.getLogger(SaveToForetellResult.class);

	//@Autowired(required = false)
	//PriceCompAverageMapper priceCompareAverageMapper;

	@Autowired(required = false)
	PriceCompAverage200Mapper priceCompareAverage200Mapper;

	@Autowired(required = false)
	IForetellResultService iForetellResultService;

	@Autowired(required = false)
	private ISTOCKService sTOCKService;




	//从临时表中统计当天预测结果
	public String from200saveToForetellResultTable() {
		List<PriceCompAverage200> list = priceCompareAverage200Mapper.selectBuyTrainByType();
		log.warn("selectBuyTrainByType:"+list.size());
		List<STOCK> stocks = sTOCKService.selectSTOCKList(null);
		Map<String, String> mindu = new HashMap<String, String>(4000);
		Map<String, String> mconc = new HashMap<String, String>(4000);
		Map<String, String> mn = new HashMap<String, String>(4000);


		for (int i = 0; i < stocks.size(); i++) {
			mindu.put(stocks.get(i).getSymbol(), stocks.get(i).getIndustry());
			mconc.put(stocks.get(i).getSymbol(), stocks.get(i).getConcept());
			mn.put(stocks.get(i).getSymbol(), stocks.get(i).getName());
		}


		for (PriceCompAverage200 obj : list) {
			//select SYMBOL, FCLOSE, F1DATE,  from PRICE_COMP_AVERAGE200
			ForetellResult foretellResult = new ForetellResult();
			foretellResult.setDatetime(obj.getF1date());
			foretellResult.setSymbol(obj.getSymbol());
			foretellResult.setDirection("buy");
			foretellResult.setModelName("Type1");
			List<ForetellResult> list1 = iForetellResultService.selectForetellResultList(foretellResult);

			foretellResult.setVolumeIndex( obj.getD1d5vol().add(obj.getE1e5vol()).add(obj.getF1f5vol()) );
			foretellResult.setBusinessTime(obj.getF1date());
			foretellResult.setBusinessPrice(obj.getFclose());
			foretellResult.setIndustry(mindu.get(obj.getSymbol()));
			foretellResult.setConcept(mconc.get(obj.getSymbol()));
			foretellResult.setName(mn.get(obj.getSymbol()));
			if(list1.size() == 0){
				iForetellResultService.insertForetellResult(foretellResult);
			}else{
				iForetellResultService.updateForetellResult(foretellResult);
			}
		}

		List<PriceCompAverage200> list2 = priceCompareAverage200Mapper.selectBuyTrainByType7();
		log.warn("selectBuyTrainByType7:"+list2.size());
		for (PriceCompAverage200 obj : list2) {
			ForetellResult foretellResult = new ForetellResult();
			foretellResult.setDatetime(obj.getF1date());
			foretellResult.setSymbol(obj.getSymbol());
			foretellResult.setDirection("buy");
			foretellResult.setModelName("Type7");
			List<ForetellResult> list1 = iForetellResultService.selectForetellResultList(foretellResult);
			foretellResult.setVolumeIndex( obj.getD1d5vol().add(obj.getE1e5vol()).add(obj.getF1f5vol()) );
			foretellResult.setBusinessTime(obj.getF1date());
			foretellResult.setBusinessPrice(obj.getFclose());
			foretellResult.setIndustry(mindu.get(obj.getSymbol()));
			foretellResult.setConcept(mconc.get(obj.getSymbol()));
			foretellResult.setName(mn.get(obj.getSymbol()));
			if(list1.size() == 0){
				iForetellResultService.insertForetellResult(foretellResult);
			}else{
				iForetellResultService.updateForetellResult(foretellResult);
			}
		}
		if(list2.size() == 0 && list.size()==0){
			return "0";
		}else{
			return "ok";
		}

	}



}
